/**
 * 
 */
package com.geekteam.geometrychess.game;

import java.util.ArrayList;
import java.util.List;

import com.geekteam.geometrychess.game.entity.Location;
import com.geekteam.geometrychess.game.entity.Piece;
import com.geekteam.geometrychess.game.entity.Piece.Color;
import com.geekteam.geometrychess.game.entity.PieceSimpleName;
import com.geekteam.geometrychess.game.entity.SimplePiece;
import com.geekteam.geometrychess.game.entity.StandardPiece;
import com.geekteam.geometrychess.utils.Constants.ChessConstants;

/**
 * @author MtRol
 * 
 */
public class ChessBoard {
	private Piece[] mPiece;
	private int[][] mBoard;
	private typeChess mType;

	public ChessBoard() {
		mType = typeChess.simple;
		initPiece();
		resetGame();
	}

	/********************************** Logic game *************************************/
	private void initPiece() {
		mBoard = new int[9][9];
		mPiece = new Piece[18];
		
		mPiece[0] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_ONE, Color.white, new Location(0, 0), PieceSimpleName.one);
		mPiece[1] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_TWO, Color.white, new Location(0, 1), PieceSimpleName.two);
		mPiece[2] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_THREE, Color.white, new Location(0, 2), PieceSimpleName.three);
		mPiece[3] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_FOUR, Color.white, new Location(0, 3), PieceSimpleName.four);
		mPiece[4] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_FIVE, Color.white, new Location(0, 4), PieceSimpleName.five);
		mPiece[5] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_SIX, Color.white, new Location(0, 5), PieceSimpleName.six);
		mPiece[6] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_SEVEN, Color.white, new Location(0, 6), PieceSimpleName.seven);
		mPiece[7] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_EIGHT, Color.white, new Location(0, 7), PieceSimpleName.eight);
		mPiece[8] = new SimplePiece(ChessConstants.WHITE_SIMPLE_ID_NINE, Color.white, new Location(0, 8), PieceSimpleName.nine);
		
		mPiece[9] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_ONE, Color.black, new Location(8, 8), PieceSimpleName.one);
		mPiece[10] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_TWO, Color.black, new Location(8, 7), PieceSimpleName.two);
		mPiece[11] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_THREE, Color.black, new Location(8, 6), PieceSimpleName.three);
		mPiece[12] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_FOUR, Color.black, new Location(8, 5), PieceSimpleName.four);
		mPiece[13] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_FIVE, Color.black, new Location(8, 4), PieceSimpleName.five);
		mPiece[14] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_SIX, Color.black, new Location(8, 3), PieceSimpleName.six);
		mPiece[15] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_SEVEN, Color.black, new Location(8, 2), PieceSimpleName.seven);
		mPiece[16] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_EIGHT, Color.black, new Location(8, 1), PieceSimpleName.eight);
		mPiece[17] = new SimplePiece(ChessConstants.DARK_SIMPLE_ID_NINE, Color.black, new Location(8, 0), PieceSimpleName.nine);
	}

	private void resetGame() {
		for(int i = 0; i<9; i++){
			for(int j =0; j<9;j++){
				mBoard[i][j] = -1;
			}
		}
		
		mBoard[0][0] = ChessConstants.WHITE_SIMPLE_ID_ONE;
		mPiece[0].setLocation(new Location(0, 0));
		mBoard[0][1] = ChessConstants.WHITE_SIMPLE_ID_TWO;
		mPiece[1].setLocation(new Location(0, 1));
		mBoard[0][2] = ChessConstants.WHITE_SIMPLE_ID_THREE;
		mPiece[2].setLocation(new Location(0, 2));
		mBoard[0][3] = ChessConstants.WHITE_SIMPLE_ID_FOUR;
		mPiece[3].setLocation(new Location(0, 3));
		mBoard[0][4] = ChessConstants.WHITE_SIMPLE_ID_FIVE;
		mPiece[4].setLocation(new Location(0, 4));
		mBoard[0][5] = ChessConstants.WHITE_SIMPLE_ID_SIX;
		mPiece[5].setLocation(new Location(0, 5));
		mBoard[0][6] = ChessConstants.WHITE_SIMPLE_ID_SEVEN;
		mPiece[6].setLocation(new Location(0, 6));
		mBoard[0][7] = ChessConstants.WHITE_SIMPLE_ID_EIGHT;
		mPiece[7].setLocation(new Location(0, 7));
		mBoard[0][8] = ChessConstants.WHITE_SIMPLE_ID_NINE;
		mPiece[8].setLocation(new Location(0, 8));
		
		mBoard[8][8] = ChessConstants.DARK_SIMPLE_ID_ONE;
		mPiece[9].setLocation(new Location(9, 8));
		mBoard[8][7] = ChessConstants.DARK_SIMPLE_ID_TWO;
		mPiece[10].setLocation(new Location(9, 7));
		mBoard[8][6] = ChessConstants.DARK_SIMPLE_ID_THREE;
		mPiece[11].setLocation(new Location(9, 6));
		mBoard[8][5] = ChessConstants.DARK_SIMPLE_ID_FOUR;
		mPiece[12].setLocation(new Location(9, 5));
		mBoard[8][4] = ChessConstants.DARK_SIMPLE_ID_FIVE;
		mPiece[13].setLocation(new Location(9, 4));
		mBoard[8][3] = ChessConstants.DARK_SIMPLE_ID_SIX;
		mPiece[14].setLocation(new Location(9, 3));
		mBoard[8][2] = ChessConstants.DARK_SIMPLE_ID_SEVEN;
		mPiece[15].setLocation(new Location(9, 2));
		mBoard[8][1] = ChessConstants.DARK_SIMPLE_ID_EIGHT;
		mPiece[16].setLocation(new Location(9, 1));
		mBoard[8][0] = ChessConstants.DARK_SIMPLE_ID_NINE;
		mPiece[17].setLocation(new Location(9, 0));
		
		//TODO reset time here
	}

	public Piece getPieceAt(int x, int y) {
		if (mBoard[x][y] != -1) {
			for (int i = 0; i < mPiece.length; i++) {
				if (mBoard[x][y] == mPiece[i].getId()) {
					return mPiece[i];
				}
			}
		}
		return null;
	}

	public Piece getPieceAt(Location location) {
		return getPieceAt(location.getPosX(), location.getPosY());
	}

	public Piece getPieceById(int id) {
		for (int i = 0; i < mPiece.length; i++) {
			if (mPiece[i].getId() == id) {
				return mPiece[i];
			}
		}
		return null;
	}

	private boolean isValidMove(Piece piece, Location dstLocation) {
		if (piece == null) {
			return false;
		}

		Piece dstPiece = getPieceAt(dstLocation);
		if (dstPiece != null) {
			return false;
		}

		if (piece.canMoveTo(this, dstLocation)) {
			return true;
		}

		return false;
	}

	private boolean isEndGame() {
		return false;
	}

	private void doMove(Piece piece, Location dstLocation) {
		// thuc hien viec di quan co
		if (piece == null) {
			return;
		}

		Location srcLocation = piece.getLocation();
		piece.setLocation(dstLocation);
		mBoard[dstLocation.getPosX()][dstLocation.getPosY()] = mBoard[srcLocation
				.getPosX()][srcLocation.getPosY()];
		mBoard[srcLocation.getPosX()][srcLocation.getPosY()] = -1;

		// ghi lai log tai day
	}

	public boolean tryMove(Piece piece, Location dstLocation) {
		if (piece == null) {
			return false;
		}

		if (isValidMove(piece, dstLocation)) {
			doMove(piece, dstLocation);
			return true;
		}

		return false;
	}

	public void checkGeometry(Piece piece) {
		if (piece == null) {
			return;
		}
		if (mType == typeChess.simple) {
			checkGeometrySimple((SimplePiece) piece);
		} else if (mType == typeChess.standard) {
			checkGeometryStandard((StandardPiece) piece);
		}
	}

	/***
	 * check co hinh chuan. khi nao gap mot hinh thi tinh toan cong diem cho
	 * nguoi choi hoac se tra ve mot mang cac quan co nhu nao do tinh sau tam
	 * thoi check da ...
	 * 
	 * thu tu check cua cac hinh se lan luot la: duong thang, tam giac, vuong
	 * 
	 */
	private void checkGeometryStandard(StandardPiece piece) {
		
	}

	/***
	 * check co hinh don gian o day. tuong tu nhu co hinh chuan.
	 * 
	 * check duong thang: khi co 3 quan co diem so lien tiep nhau va cach deu
	 * nhau se dc tinh 1 diem. Neu nhu co 4 quan tro nen tao thanh duong thang
	 * thi se dc cong 2 diem
	 * 
	 * check tam giac, ba con phai co so diem lien tiep xep xoay vong nguoc hoac
	 * xuoi deu dc.
	 * 
	 * check hinh vuong: bon con phai co so diem lien tiep xep lien tuc, xoay
	 * vong nguoc hoac xuoi deu dc. xep hinh vuong dc tinh 2 diem
	 * 
	 * check da hinh: la cac hinh don le, co su dung chung quan vua di hoac mot
	 * canh chua quan vua di. so diem thu dc la tong cac hinh don le
	 * 
	 */
	private int mNumberGeo;// so hinh tao duoc
	private int mPoint;// cai nay la tinh so diem tao nen hinh do

	private void checkGeometrySimple(SimplePiece piece) {
		mNumberGeo = 0;
		mPoint = 0;
	}

	/**
	 * tu quan co vua di se thuc hien viec lan vet theo cac huong ngang, doc,
	 * cheo trai, cheo phai. Khi gap 1 quan co. neu co point = + hoac - 1 diem
	 * so vs quan hien tai thi check tiep bang cach xac dinh diem tiep theo vs
	 * buoc di bang buoc di cua vua tinh duoc. Neu co quan co thi check tiep
	 * khong thi thoi. Duyet ve phia nguoc lai theo phuong phap do.
	 * */
	private void checkStraightLineSimple(SimplePiece piece) {
		// check theo hang ngang
		int step = 0;
		int x = piece.getLocation().getPosX();
		int y = piece.getLocation().getPosY();
		x++;
		while (x > 0 && x < 9) {
			step++;
			SimplePiece destPiece = (SimplePiece) getPieceAt(x, y);
			if (destPiece != null) {
				// neu nhu abs cua 2 quan canh nhau la 1 thi se dc check tiep
				if (Math.abs(destPiece.getPoint() - piece.getPoint()) == 1) {
					List<SimplePiece> listPiece = checkCrossLineSimple(piece,
							destPiece, step);
					if (isGeometryColor(piece, listPiece, 2)) {
						// day la mot duong thang dc chap nhan

					}
				} else {
					// khong co duong thang duoc tao ra
				}
				break;
			}
			x++;
		}

		// chek theo hang doc
		step = 0;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y++;
		while (y > 0 && y < 9) {
			step++;
			SimplePiece destPiece = (SimplePiece) getPieceAt(x, y);
			if (destPiece != null) {
				// neu nhu abs cua 2 quan canh nhau la 1 thi se dc check tiep
				if (Math.abs(destPiece.getPoint() - piece.getPoint()) == 1) {
					List<SimplePiece> listPiece = checkStemLineSimple(piece,
							destPiece, step);
					if (isGeometryColor(piece, listPiece, 2)) {
						// day la mot duong thang dc chap nhan

					}
				} else {
					// khong co duong thang duoc tao ra
				}
				break;
			}
			y++;
		}

		// check cheo trai
		step = 0;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y++;
		x--;
		while (x > 0 && x < 9 && y > 0 && y < 9) {
			step++;
			SimplePiece destPiece = (SimplePiece) getPieceAt(x, y);
			if (destPiece != null) {
				if (Math.abs(destPiece.getPoint() - piece.getPoint()) == 1) {
					List<SimplePiece> listPiece = checkDiagonalLeftLineSimple(
							piece, destPiece, step);
					if (isGeometryColor(piece, listPiece, 2)) {
						// day la mot duong thang dc chap nhan

					}
				} else {

				}
				break;
			}
			y++;
			x--;
		}

		// check cheo phai
		step = 0;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y++;
		x++;
		while (x > 0 && x < 9 && y > 0 && y < 9) {
			step++;
			SimplePiece destPiece = (SimplePiece) getPieceAt(x, y);
			if (destPiece != null) {
				if (Math.abs(destPiece.getPoint() - piece.getPoint()) == 1) {
					List<SimplePiece> listPiece = checkDiagonalRightLineSimple(
							piece, destPiece, step);
					if (isGeometryColor(piece, listPiece, 2)) {
						// day la mot duong thang dc chap nhan

					}
				} else {

				}
				break;
			}
			y++;
			x++;
		}
	}

	private boolean isGeometryColor(SimplePiece piece,
			List<SimplePiece> listPiece, int threshold) {
		if (listPiece.size() > threshold) {
			int myPiece = 0;
			for (SimplePiece p : listPiece) {
				if (p.getColor() == piece.getColor()) {
					myPiece++;
				}
			}
			if (myPiece >= (listPiece.size() / 2)) {
				return true;
			}
		}
		return false;
	}

	// check duong thang theo chieu ngang
	private List<SimplePiece> checkCrossLineSimple(SimplePiece piece,
			SimplePiece firstPiece, int step) {
		List<SimplePiece> listPieces = new ArrayList<SimplePiece>();
		listPieces.add(piece);
		listPieces.add(firstPiece);
		int space = 2;
		// check viec di tiep
		SimplePiece temp = firstPiece;
		int x = firstPiece.getLocation().getPosX();
		int y = firstPiece.getLocation().getPosY();
		x += step;
		while (x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(piece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x - 1, y))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			x += step;
		}

		// check viec di ve huong doi dien
		space = 2;
		temp = piece;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		x -= step;
		while (x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(firstPiece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x - 1, y))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			x -= step;
		}

		return listPieces;
	}

	// chec duong thang theo chieu doc
	private List<SimplePiece> checkStemLineSimple(SimplePiece piece,
			SimplePiece firstPiece, int step) {
		List<SimplePiece> listPieces = new ArrayList<SimplePiece>();
		listPieces.add(piece);
		listPieces.add(firstPiece);
		int space = 2;

		// check viec di tiep
		SimplePiece temp = firstPiece;
		int x = firstPiece.getLocation().getPosX();
		int y = firstPiece.getLocation().getPosY();
		y += step;
		while (y > 0 && y < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(piece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x, y - 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y += step;
		}

		// check viec di ve huong doi dien
		space = 2;
		temp = piece;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y -= step;
		while (y > 0 && y < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(firstPiece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x, y - 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y -= step;
		}
		return listPieces;
	}

	private List<SimplePiece> checkDiagonalLeftLineSimple(SimplePiece piece,
			SimplePiece firstPiece, int step) {
		List<SimplePiece> listPieces = new ArrayList<SimplePiece>();
		listPieces.add(piece);
		listPieces.add(firstPiece);
		int space = 2;

		SimplePiece temp = firstPiece;
		int x = firstPiece.getLocation().getPosX();
		int y = firstPiece.getLocation().getPosY();
		y += step;
		x -= step;
		while (y > 0 && y < 9 && x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(piece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x + 1, y - 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y += step;
			x -= step;
		}

		space = 2;
		temp = piece;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y -= step;
		x += step;
		while (y > 0 && y < 9 && x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(firstPiece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x - 1, y + 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y -= step;
			x += step;
		}

		return listPieces;
	}

	private List<SimplePiece> checkDiagonalRightLineSimple(SimplePiece piece,
			SimplePiece firstPiece, int step) {
		List<SimplePiece> listPieces = new ArrayList<SimplePiece>();
		listPieces.add(piece);
		listPieces.add(firstPiece);
		int space = 2;

		SimplePiece temp = firstPiece;
		int x = firstPiece.getLocation().getPosX();
		int y = firstPiece.getLocation().getPosY();
		y += step;
		x += step;
		while (y > 0 && y < 9 && x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(piece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x - 1, y - 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y += step;
			x += step;
		}

		space = 2;
		temp = piece;
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y -= step;
		x -= step;
		while (y > 0 && y < 9 && x > 0 && x < 9) {
			SimplePiece nextPiece = (SimplePiece) getPieceAt(x, y);
			if (nextPiece == null) {
				break;
			}

			if (Math.abs(temp.getPoint() - nextPiece.getPoint()) == 1
					&& Math.abs(firstPiece.getPoint() - nextPiece.getPoint()) == space) {
				if (temp.canMoveTo(this, new Location(x + 1, y + 1))) {
					listPieces.add(nextPiece);
					temp = nextPiece;
					space++;
				} else {
					break;
				}
			} else {
				break;
			}
			y -= step;
			x -= step;
		}

		return listPieces;
	}

	private void checkTriangleSimple(SimplePiece piece) {
		List<SimplePiece> listNearPieces = getListNearPiece(piece, 2);// 2 vi
																		// tam
																		// giac
																		// co so
																		// diem
																		// sai
																		// lech
																		// la 2
		for (SimplePiece p : listNearPieces) {
			if (piece.getPoint() - p.getPoint() == -2) {
				for (SimplePiece p1 : listNearPieces) {
					if (piece.getPoint() - p1.getPoint() == -1) {
						checkTriangle(piece, p, p1);
					}
				}
			}

			if (piece.getPoint() - p.getPoint() == -1) {
				for (SimplePiece p1 : listNearPieces) {
					if ((piece.getPoint() - p1.getPoint() == -2)
							|| (piece.getPoint() - p1.getPoint() == 1)) {
						checkTriangle(piece, p, p1);
					}
				}
			}

			if (piece.getPoint() - p.getPoint() == 1) {
				for (SimplePiece p1 : listNearPieces) {
					if ((piece.getPoint() - p1.getPoint() == 2)
							|| (piece.getPoint() - p1.getPoint() == -1)) {
						checkTriangle(piece, p, p1);
					}
				}
			}

			if (piece.getPoint() - p.getPoint() == 2) {
				for (SimplePiece p1 : listNearPieces) {
					if ((piece.getPoint() - p1.getPoint() == 1)) {
						checkTriangle(piece, p, p1);
					}
				}
			}
		}
	}

	private void checkTriangle(SimplePiece piece, SimplePiece p, SimplePiece p1) {
		int tempBoard = mBoard[p1.getLocation().getPosX()][p1.getLocation()
				.getPosY()];
		mBoard[p1.getLocation().getPosX()][p1.getLocation().getPosY()] = -1;
		if (p.canMoveTo(this, new Location(p1.getLocation().getPosX(), p1
				.getLocation().getPosY()))) {
			List<SimplePiece> listTriangle = new ArrayList<SimplePiece>();
			listTriangle.add(piece);
			listTriangle.add(p1);
			listTriangle.add(p);
			if (isGeometryColor(piece, listTriangle, 2)) {
				// cho nay la mot hinh tam giac
			}
		}
		mBoard[p1.getLocation().getPosX()][p1.getLocation().getPosY()] = tempBoard;
	}

	private List<SimplePiece> getListNearPiece(SimplePiece piece, int subtract) {
		int x = piece.getLocation().getPosX();
		int y = piece.getLocation().getPosY();
		List<SimplePiece> listNearPieces = new ArrayList<SimplePiece>();
		// check ngang
		x++;
		while (x > 0 && x < 9) {
			SimplePiece dstPiece = (SimplePiece) getPieceAt(x, y);
			if (dstPiece != null) {
				if (checkNearPoint(piece, dstPiece, subtract)) {
					listNearPieces.add(dstPiece);
				}
				// check nguoc lai
				x = piece.getLocation().getPosX();
				x--;
				while (x > 0 && x < 9) {
					dstPiece = (SimplePiece) getPieceAt(x, y);
					if (dstPiece != null) {
						if (checkNearPoint(piece, dstPiece, subtract)) {
							listNearPieces.add(dstPiece);
						}
						break;
					}
					x--;
				}
				break;
			}
			x++;
		}

		// check doc
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		y++;
		while (y > 0 && y < 9) {
			SimplePiece dstPiece = (SimplePiece) getPieceAt(x, y);
			if (dstPiece != null) {
				if (checkNearPoint(piece, dstPiece, subtract)) {
					listNearPieces.add(dstPiece);
				}
				// check nguoc lai
				y = piece.getLocation().getPosY();
				y--;
				while (y > 0 && y < 9) {
					dstPiece = (SimplePiece) getPieceAt(x, y);
					if (dstPiece != null) {
						if (checkNearPoint(piece, dstPiece, subtract)) {
							listNearPieces.add(dstPiece);
						}
						break;
					}
					y--;
				}
				break;
			}
			y++;
		}

		// check cheo trai
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		x--;
		y++;
		while (x > 0 && x < 9 && y > 0 && y < 9) {
			SimplePiece dstPiece = (SimplePiece) getPieceAt(x, y);
			if (dstPiece != null) {
				if (checkNearPoint(piece, dstPiece, subtract)) {
					listNearPieces.add(dstPiece);
				}
				x = piece.getLocation().getPosX();
				y = piece.getLocation().getPosY();
				x++;
				y--;
				while (x > 0 && x < 9 && y > 0 && y < 9) {
					dstPiece = (SimplePiece) getPieceAt(x, y);
					if (dstPiece != null) {
						if (checkNearPoint(piece, dstPiece, subtract)) {
							listNearPieces.add(dstPiece);
						}
						break;
					}
					x++;
					y--;
				}
				break;
			}
			x--;
			y++;
		}

		// check cheo phai
		x = piece.getLocation().getPosX();
		y = piece.getLocation().getPosY();
		x++;
		y++;
		while (x > 0 && x < 9 && y > 0 && y < 9) {
			SimplePiece dstPiece = (SimplePiece) getPieceAt(x, y);
			if (dstPiece != null) {
				if (checkNearPoint(piece, dstPiece, subtract)) {
					listNearPieces.add(dstPiece);
				}
				x = piece.getLocation().getPosX();
				y = piece.getLocation().getPosY();
				x--;
				y--;
				while (x > 0 && x < 9 && y > 0 && y < 9) {
					dstPiece = (SimplePiece) getPieceAt(x, y);
					if (dstPiece != null) {
						if (checkNearPoint(piece, dstPiece, subtract)) {
							listNearPieces.add(dstPiece);
						}
						break;
					}
					x--;
					y--;
				}
				break;
			}
			x++;
			y++;
		}

		return listNearPieces;
	}

	private boolean checkNearPoint(SimplePiece piece, SimplePiece dstPiece,
			int subtract) {
		return (dstPiece != null
				&& Math.abs(dstPiece.getPoint() - piece.getPoint()) <= subtract && Math
				.abs(dstPiece.getPoint() - piece.getPoint()) > 0);

	}

	private void checkSquareSimple(Piece piece) {

	}

	private void checkMultiGeometrySimple(Piece piece) {

	}

	private void endGame() {

	}

	/********************************** enum *************************************/
	public enum typeChess {
		standard, simple
	}

	/********************************** return object *************************************/

}
